package org.hobbyhorse.ranks.client;

import org.hobbyhorse.gaerequest.client.GaeAuthRequestTransport;
import org.hobbyhorse.gaerequest.client.ReloadOnAuthenticationFailure;
import org.hobbyhorse.ranks.client.desktop.RanksAppShellDesktop;
import org.hobbyhorse.ranks.client.mapper.AppPlaceHistoryMapper;
import org.hobbyhorse.ranks.client.mapper.RightPanelActivityMapper;
import org.hobbyhorse.ranks.shared.RanksRequestFactory;

import com.google.gwt.activity.shared.ActivityManager;
import com.google.gwt.activity.shared.ActivityMapper;
import com.google.gwt.core.client.GWT;
import com.google.gwt.place.shared.PlaceController;
import com.google.gwt.place.shared.PlaceHistoryHandler;
import com.google.gwt.storage.client.Storage;
import com.google.gwt.user.client.Window;
import com.google.web.bindery.event.shared.EventBus;
import com.google.web.bindery.event.shared.SimpleEventBus;
import com.google.web.bindery.requestfactory.shared.RequestTransport;


public class ClientFactoryImpl implements ClientFactory {

	  /**
	   * The URL argument used to enable or disable local storage.
	   */
	  private static final String STORAGE_URL_ARG = "storage";

    private final EventBus eventBus = new SimpleEventBus();
    private final PlaceController placeController = new PlaceController(eventBus);
    private final Storage localStorage;
    private final RanksRequestFactory requestFactory;
    private ActivityManager activityManager;
    private RanksAppShell shell;

    private final AppPlaceHistoryMapper historyMapper = GWT.create(AppPlaceHistoryMapper.class);
    /**
     * The stock GWT class that ties the PlaceController to browser history,
     * configured by our custom {@link #historyMapper}.
     */
    private final PlaceHistoryHandler historyHandler = new PlaceHistoryHandler(historyMapper);

    public ClientFactoryImpl() {
        RequestTransport requestTransport = new GaeAuthRequestTransport(eventBus);
        requestFactory = GWT.create(RanksRequestFactory.class);
        requestFactory.initialize(eventBus, requestTransport);
        // Initialize local storage.
        String storageUrlValue = Window.Location.getParameter(STORAGE_URL_ARG);
        if (storageUrlValue == null || storageUrlValue.startsWith("t")) {
          localStorage = Storage.getLocalStorageIfSupported();
        } else {
          localStorage = null;
        }
//        taskProxyLocalStorage = new TaskProxyLocalStorage(localStorage);
	}


    
    /*    private final HorsesViewImpl  horsesPanel = new HorsesViewImpl(this);
    private static final NewHorseViewImpl  newHorsePanel = new NewHorseViewImpl();
    private static final RidersPanel  ridersPanel = new RidersPanel();
*/
	@Override
    public EventBus getEventBus() {
		return eventBus;
	}

	public PlaceController getPlaceController() {
		return placeController;
	}

	public RanksRequestFactory getRequestFactory() {
		return requestFactory;
	}


	public RanksAppShell getShell() {
	    if (shell == null) {
	      shell = createShell();
	    }
	    return shell;
	}
	
	/*
	public HorsesViewImpl getHorsesView() {
		return horsesPanel;
	}

	public RidersPanel getRidersPanel() {
		return ridersPanel;
	}

	public NewHorseViewImpl getNewHorseView() {
		return newHorsePanel;
	}
*/

	@Override
	public App getApp() {
	    return new App(getLocalStorageIfSupported(), eventBus, getPlaceController(),
	            getActivityManager(), historyMapper, historyHandler, new ReloadOnAuthenticationFailure(),
	            getShell());
	}

	/**
	* ActivityMapper determines an Activity to run for a particular place,
	* configures the {@link #getActivityManager()}
	*/
	  protected ActivityMapper createActivityMapper() {
	    return new RightPanelActivityMapper(this);
	  }
	
	/**
	   * Owns a panel in the window, in this case the entire {@link #shell}.
	   * Monitors the {@link #eventBus} for
	   * {@link com.google.gwt.place.shared.PlaceChangeEvent PlaceChangeEvent}s posted by the
	   * {@link #placeController}, and chooses what
	   * {@link com.google.gwt.activity.shared.Activity Activity} gets to take
	   * over the panel at the current place. Configured by the
	   * {@link #createActivityMapper()}.
	   */
	  protected ActivityManager getActivityManager() {
	    if (activityManager == null) {
	      activityManager = new ActivityManager(createActivityMapper(), eventBus);
	    }
	    return activityManager;
	  }
	
	private Storage getLocalStorageIfSupported() {
		    return localStorage;
	}
	  /**
	   * Create the application UI shell.
	   * 
	   * @return the UI shell
	   */
	protected RanksAppShell createShell() {
		return new RanksAppShellDesktop(this,placeController);
	}


}
